if (!String.prototype.contains) {
    String.prototype.contains = function() {
        return String.prototype.indexOf.apply( this, arguments ) !== -1;
    };
}

if (!String.prototype.isEmail) {
	String.prototype.isEmail = function() {
		var pattern = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@" +
					  "(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|" +
					  "biz|info|mobi|name|aero|asia|jobs|museum)\\b";

		var email = this.toLowerCase();
		var regEx = new RegExp(pattern);
		var match = regEx.exec(email);
		return (match == email);
	};
}

if (!String.prototype.trim) {
  String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g, '');
  };
}

if (!Date.prototype.diff) {
	Date.prototype.diff = function(date, interval) {
		var option = 'day';
		var divisor = 60000 * 60;

		var possibleOptions = [ 
			{ name: 'millisecond', divisor: 1 },
			{ name: 'second', divisor: 1000 },
			{ name: 'minute', divisor: 60000 },
			{ name: 'hour', divisor: 60000 * 60 },
			{ name: 'day', divisor: (60000 * 60) * 24 },
			{ name: 'week', divisor: ((60000 * 60) * 24) * 7 },
			{ name: 'year', divisor: ((60000 * 60) * 24) * 365 }
		];

		if (interval != undefined) {
			if (interval != null) {
				option = interval.trim().toLowerCase();
			
				for (i = 0; i < possibleOptions.length; i++) {
					if (possibleOptions[i].name == option) {
						divisor = possibleOptions[i].divisor;
						break;
					}
				}
			}
		}

		var diff = this - date;
		return Math.floor(diff / divisor);
	};
}

if (!Date.prototype.toSqlDateString) {
	Date.prototype.toSqlDateString = function() {
		var dated = this;

		var day = dated.getDate();
		var month = dated.getMonth() + 1;
		var year = dated.getFullYear();
		var hour = dated.getHours();
		var minute = dated.getMinutes();
		var second = dated.getSeconds();

		var sqlFormat = year.toString();
		sqlFormat += '-' + (month <= 9? '0' : '') + month.toString();
		sqlFormat += '-' + (day <= 9? '0' : '') + day.toString();
		sqlFormat += ' ' + (hour <= 9? '0' : '') + hour.toString();
		sqlFormat += ':' + (minute <= 9? '0' : '') + minute.toString();
		sqlFormat += ':' + (second <= 9? '0' : '') + second.toString();
		
		return sqlFormat;
	};
}

var Application = function() {

};

var app = new Application();

app.baseUrl = function() {
	var brand = $('a.navbar-brand.nav-bar-menu');
	return brand.attr('href');
};

app.loader = function(options) {
	var hidden = false;
	var container = null;

	if (options != undefined) {
		if (options != null) {
			hidden = (options.toString().toLowerCase().trim() == 'hide');

			if (!hidden) {
				if (options.container != undefined) {
					if (options.container != null) container = options.container;
				}

				if (options.visible != undefined) {
					if (options.visible != null) hidden = !options.visible;
				}
			}
		}
	}

	var loaderDiv = null;
	if (container == null) container = $('body');

	loaderDiv = container.find('.animated-bg-loader');

	if (hidden) {
		if (loaderDiv.length > 0) loaderDiv.remove();	
	} 
	else {
		if (loaderDiv.length <= 0) container.append('<div class="animated-bg-loader">\n' +
													'	<img src="' + app.baseUrl() + 'img/loader.gif" alt="Loading...">\n' + 
													'</div>');
	}
};